;; Kostas Oikonomou, September 1993. ;; Removed the support for plain TeX, which didn't work anyway. ;; Added a new function make-file, based on tex-file. This function is used by noweb-mode. ;; Kostas Oikonomou, December 1992. ;; This is hacked from tex-mode.el. A lot of code has been removed. ;; Besides that, the most notable modifications are that the functions latex-mode ;; and tex-mode (which conflict with the functions in Beebe's latex.el) have been ;; renamed setup-latex and setup-tex. These functions must be called by LaTeX-mode-hook ;; and TeX-mode-hook for things to work properly. ;; Other modifications: ;; 1) A line has been added to the function tex-file. ;; 2) The "-v" has been deleted from the make-comint line. ;; 3) Two lines have been added near the end of tex-file to keep the output ;; in *tex-shell* visible. Same was done to tex-region. ;; 4) The function tex-delete-temp-files has been made interactive. ;; Still to do: ;; a) Use re-search for tex-start-of-header, etc., so both "\begin{document}" and ;; "\begin {document}" can be used. (require 'comint) (provide 'tex-support) (defvar tex-shell-file-name nil "*If non-nil, is file name to use for the subshell in which TeX is run.") (defvar tex-directory "." "*Directory in which temporary files are left. You can make this /tmp if your TEXINPUTS has no relative directories in it and you don't try to apply \\[tex-region] or \\[tex-buffer] when there are \\input commands with relative directories.") (defvar latex-run-command "latex" "*Command used to run LaTeX subjob. If this string contains an asterisk (*), it will be replaced by the filename; if not, the name of the file, preceded by blank, will be added to this string.") (defvar tex-dvi-print-command "lpr -d" "*Command used by \\[tex-print] to print a .dvi file. If this string contains an asterisk (*), it will be replaced by the filename; if not, the name of the file, preceded by blank, will be added to this string.") (defvar tex-alt-dvi-print-command "lpr -d" "*Command used by \\[tex-print] with a prefix arg to print a .dvi file. If this string contains an asterisk (*), it will be replaced by the filename; if not, the name of the file, preceded by blank, will be added to this string. If two printers are not enough of a choice, you can define the value of tex-alt-dvi-print-command to be an expression that asks what you want; for example, (setq tex-alt-dvi-print-command '(format \"lpr -P%s\" (read-string \"Use printer: \"))) would tell \\[tex-print] with a prefix argument to ask you which printer to use.") (defvar tex-dvi-view-command nil "*Command used by \\[tex-view] to display a .dvi file. If this string contains an asterisk (*), it will be replaced by the filename; if not, the name of the file, preceded by blank, will be added to this string. This can be set conditionally so that the previewer used is suitable for the window system being used. For example, (setq tex-dvi-view-command (if (eq window-system 'x) \"xdvi\" \"dvi2tty * | cat -s\")) would tell \\[tex-view] use xdvi under X windows and to use dvi2tty otherwise.") (defvar tex-last-temp-file nil "Latest temporary file generated by \\[tex-region] and \\[tex-buffer]. Deleted when the \\[tex-region] or \\[tex-buffer] is next run, or when the tex-shell goes away.") (defvar tex-command nil "Command to run TeX. The name of the file, preceded by a blank, will be added to this string.") (defvar tex-trailer nil "String appended after the end of a region sent to TeX by \\[tex-region].") (defvar tex-start-of-header nil "String used by \\[tex-region] to delimit the start of the file's header.") (defvar tex-end-of-header nil "String used by \\[tex-region] to delimit the end of the file's header.") (defvar tex-shell-cd-command "cd" "Command to give to shell running TeX to change directory. The value of tex-directory will be appended to this, separated by a space.") (defvar tex-zap-file nil "Temporary file name used for text being sent as input to TeX. Should be a simple file name with no extension or directory specification.") (defvar tex-last-buffer-texed nil "Buffer which was last TeXed.") (defvar tex-print-file nil "File name that \\[tex-print] prints. Set by \\[tex-region], \\[tex-buffer], and \\[tex-file].") (defvar tex-shell-map nil "Keymap for the tex-shell. A comint-mode-map with a few additions.") (defun setup-latex () "Give various variables values appropriate for LaTeX mode" (interactive) (tex-common-initialization) (setq tex-command latex-run-command) (setq tex-start-of-header "\\documentstyle") (setq tex-end-of-header "\\begin {document}") (setq tex-trailer "\\end {document}\n") (defun tex-common-initialization () (make-local-variable 'tex-command) (make-local-variable 'tex-start-of-header) (make-local-variable 'tex-end-of-header) (make-local-variable 'tex-trailer)) ;;; Invoking TeX in an inferior shell. ;;; Why use a shell instead of running TeX directly? Because if TeX ;;; gets stuck, the user can switch to the shell window and type at it. ;;; The utility functions: (defun tex-start-shell () (save-excursion (set-buffer (make-comint "tex-shell" (or tex-shell-file-name (getenv "ESHELL") (getenv "SHELL") "/bin/sh"))) (let ((proc (get-process "tex-shell"))) (set-process-sentinel proc 'tex-shell-sentinel) (process-kill-without-query proc) (setq tex-shell-map (copy-keymap comint-mode-map)) (while (zerop (buffer-size)) (sleep-for 1))))) (defun tex-shell-sentinel (proc msg) (cond ((null (buffer-name (process-buffer proc))) ;; buffer killed (set-process-buffer proc nil) (tex-delete-last-temp-files)) ((memq (process-status proc) '(signal exit)) (tex-delete-last-temp-files)))) (defun tex-set-buffer-directory (buffer directory) "Set BUFFER's default directory to be DIRECTORY." (setq directory (file-name-as-directory (expand-file-name directory))) (if (not (file-directory-p directory)) (error "%s is not a directory" directory) (save-excursion (set-buffer buffer) (setq default-directory directory)))) (defun tex-send-command (command &optional file background) "Send COMMAND to tex-shell, substituting optional FILE for *; in background if optional BACKGROUND is t. If COMMAND has no *, FILE will be appended, preceded by a blank, to COMMAND. If FILE is nil, no substitution will be made in COMMAND. COMMAND can be any expression that evaluates to a command string." (save-excursion (let* ((cmd (eval command)) (star (string-match "\\*" cmd))) (comint-proc-query (get-process "tex-shell") (concat (substring cmd 0 star) (if file (concat " " file) "") (if star (substring cmd (1+ star) nil) "") (if background "&\n" "\n")))))) (defun tex-delete-last-temp-files () "Delete any junk files from last temp file." (interactive) (if tex-last-temp-file (let* ((dir (file-name-directory tex-last-temp-file)) (list (file-name-all-completions (file-name-nondirectory tex-last-temp-file) dir))) (while list (delete-file (concat dir (car list))) (setq list (cdr list)))))) (setq kill-emacs-hook 'tex-delete-last-temp-files) ;;; The commands: (defun tex-region (beg end) "Run TeX on the current region, via a temporary file. The file's name comes from the variable `tex-zap-file' and the variable `tex-directory' says where to put it. If the buffer has a header, the header is given to TeX before the region itself. The buffer's header is all lines between the strings defined by `tex-start-of-header' and `tex-end-of-header' inclusive. The header must start in the first 100 lines of the buffer. The value of `tex-trailer' is given to TeX as input after the region. The value of `tex-command' specifies the command to use to run TeX." (interactive "r") (if (tex-shell-running) (tex-kill-job) (tex-start-shell)) (or tex-zap-file (setq tex-zap-file (tex-generate-zap-file-name))) (let* ((temp-buffer (get-buffer-create " TeX-Output-Buffer")) ; Temp file will be written and TeX will be run in zap-directory. ; If the TEXINPUTS file has relative directories or if the region has ; \input of files, this must be the same directory as the file for ; TeX to access the correct inputs. That's why it's safest if ; tex-directory is ".". (zap-directory (file-name-as-directory (expand-file-name tex-directory))) (tex-out-file (concat zap-directory tex-zap-file))) (tex-delete-last-temp-files) ;; Write the new temp file. (save-excursion (save-restriction (widen) (goto-char (point-min)) (forward-line 100) (let ((search-end (point)) (hbeg (point-min)) (hend (point-min)) (default-directory zap-directory)) (goto-char (point-min)) ;; Initialize the temp file with either the header or nothing (if (search-forward tex-start-of-header search-end t) (progn (beginning-of-line) (setq hbeg (point)) ;mark beginning of header (if (search-forward tex-end-of-header nil t) (progn (forward-line 1) (setq hend (point))) ;mark end of header (setq hbeg (point-min))))) ;no header (write-region (min hbeg beg) hend (concat tex-out-file ".tex") nil nil) (write-region (max beg hend) end (concat tex-out-file ".tex") t nil)) (let ((local-tex-trailer tex-trailer)) (set-buffer temp-buffer) (erase-buffer) ;; make sure trailer isn't hidden by a comment (insert "\n") (if local-tex-trailer (insert local-tex-trailer)) (tex-set-buffer-directory temp-buffer zap-directory) (write-region (point-min) (point-max) (concat tex-out-file ".tex") t nil)))) ;; Record the file name to be deleted afterward. (setq tex-last-temp-file tex-out-file) (tex-send-command tex-shell-cd-command zap-directory) (tex-send-command tex-command tex-out-file) (pop-to-buffer "*tex-shell*") ; Added by K.O. (goto-char (point-max)) ; Added by K.O. (setq tex-print-file tex-out-file) (setq tex-last-buffer-texed (current-buffer)))) (defun tex-buffer () "Run TeX on current buffer. See \\[tex-region] for more information. Does not save the buffer, so it's useful for trying experimental versions. See \\[tex-file] for an alternative." (interactive) (tex-region (point-min) (point-max))) (defun tex-file () "Run TeX (or LaTeX) on current buffer's file. This function is more useful than \\[tex-buffer] when you need the `.aux' file of LaTeX to have the correct name." (interactive) (let ((tex-out-file (if (buffer-file-name) (file-name-nondirectory (buffer-file-name)) (error "Buffer does not seem to be associated with any file"))) (file-dir (file-name-directory (buffer-file-name)))) (basic-save-buffer) ; Added by K.O. (if (tex-shell-running) (tex-kill-job) (tex-start-shell)) (tex-send-command tex-shell-cd-command file-dir) (tex-send-command tex-command tex-out-file) (pop-to-buffer "*tex-shell*") ; Added by K.O. (goto-char (point-max)) ; Added by K.O. (setq tex-last-buffer-texed (current-buffer)) (setq tex-print-file (buffer-file-name)) (defun make-file () "Modification of tex-file, by leaving out the tex-out-file from tex-send-command" (interactive) (basic-save-buffer) ; Added by K.O. (if (tex-shell-running) (tex-kill-job) (tex-start-shell)) (tex-send-command tex-shell-cd-command (file-name-directory (buffer-file-name))) (tex-send-command tex-command) (pop-to-buffer "*tex-shell*") ; Added by K.O. (goto-char (point-max)) ; Added by K.O. (setq tex-last-buffer-texed (current-buffer)) (setq tex-print-file (buffer-file-name)) (defun tex-generate-zap-file-name () "Generate a unique name suitable for use as a file name." ;; Include the shell process number and host name ;; in case there are multiple shells (for same or different user). (format "#tz%d%s" (process-id (get-buffer-process "*tex-shell*")) (tex-strip-dots (system-name)))) (defun tex-strip-dots (s) (setq s (copy-sequence s)) (while (string-match "\\." s) (aset s (match-beginning 0) ?-)) ;; This will perhaps be useful for modifying TEXINPUTS. ;; Expand each file name, separated by colons, in the string S. (defun tex-expand-files (s) (let (elts (start 0)) (while (string-match ":" s start) (setq elts (cons (substring s start (match-beginning 0)) elts)) (setq start (match-end 0))) (or (= start 0) (setq elts (cons (substring s start) elts))) (mapconcat 'expand-file-name (nreverse elts) ":"))) (defun tex-shell-running () (and (get-process "tex-shell") (eq (process-status (get-process "tex-shell")) 'run))) (defun tex-kill-job () "Kill the currently running TeX job." (interactive) (quit-process (get-process "tex-shell") t)) (defun tex-print (&optional alt) "Print the .dvi file made by \\[tex-region], \\[tex-buffer] or \\[tex-file]. Runs the shell command defined by tex-dvi-print-command. If prefix argument is provided, use the alternative command, tex-alt-dvi-print-command." (interactive "P") (let ((print-file-name-dvi (tex-append tex-print-file ".dvi")) test-name) (if (and (not (equal (current-buffer) tex-last-buffer-texed)) (file-newer-than-file-p (setq test-name (tex-append (buffer-file-name) ".dvi")) print-file-name-dvi)) (setq print-file-name-dvi test-name)) (if (not (file-exists-p print-file-name-dvi)) (error "No appropriate `.dvi' file could be found") (tex-send-command (if alt tex-alt-dvi-print-command tex-dvi-print-command) print-file-name-dvi t)))) (defun tex-view () "Preview the last `.dvi' file made by running TeX under Emacs. This means, made using \\[tex-region], \\[tex-buffer] or \\[tex-file]. The variable `tex-dvi-view-command' specifies the shell command for preview." (interactive) (let ((tex-dvi-print-command tex-dvi-view-command)) (tex-print))) (defun tex-append (file-name suffix) "Append to FILENAME the suffix SUFFIX, using same algorithm TeX uses. Scans for the first (not last) period. No period is retained immediately before SUFFIX, so normally SUFFIX starts with one." (if (stringp file-name) (let ((file (file-name-nondirectory file-name))) (concat (file-name-directory file-name) (substring file 0 (string-match "\\." file)) suffix)) " "))